<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
// \App\Console\Commands\CreateInitCommand::class

class CreateInitCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'create:init';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'create init';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }


    public function publish($src)
    {
        $target = app_path().$src;
        $src = dirname(__FILE__)."/stubs".$src.".stub";
        
        $init_path = dirname($target);

        $source = file_get_contents($src) ?? '';

        // 创建
        if (file_exists($init_path) === false) {
            if (mkdir($init_path, 0777, true) === false) {
                $this->error('目录' . $init_path . '没有写入权限');
                exit;
            }
        }

        // 写入
        if (!file_put_contents($target, $source)) {
            $this->error($target.'创建失败！');
            exit;
        }

        $this->info($target.'创建成功！');
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $pathArr = [
            '/Models/Permission.php',
            '/Models/Role.php',
            '/Exceptions/ApiException.php',
            '/Exceptions/ForbiddenException.php',
            '/Exceptions/Handler.php',
        ];
        
        foreach($pathArr as $path){ 
            $this->publish($path);
        } 

    }
}













